import {getDetail, getCommentList, createComment} from "@/services/topic";
import {message} from "antd";
//  文章详情
const namespace = 'topicDetail';
const selectState = state => state[namespace];

const Model = {
  namespace,
  state: {
    id: null,
    topic: {},
    comments: [],
    editingComment: ''
  },
  effects: {
    * getDetail(_, {call, select, put}) {
      const {id} = yield select(selectState);
      const res = yield call(getDetail, id);
      if (res.err === 0) {
        const topic = res.data;
        yield put({type: 'overrideStateProps', payload: {topic}})
      } else {
        message.error(res.msg)
      }

    },
    // * getComments(_, {call, select, put}) {
    //   const {id} = yield select(selectState);
    //   const comments = yield call(getCommentList, id);
    //   yield put({type: 'overrideStateProps', payload: {comments}})
    // },
    * createComment(_, {call, select, put}) {
      const {id, editingComment} = yield select(selectState);
      const res = yield call(createComment, id, {content: editingComment});
      if (res.err === 0) {
        message.success('评论成功')
      } else {
        message.error(res.msg)
      }
      yield put({type: 'getComments'})
    }
  },
  reducers: {
    overrideStateProps(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    }
  }
};

export default Model
